//
//  CartoonDetailLIistViewController.m
//  WuLi
//
//  Created by Gollum on 16/9/25.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "CartoonDetailLIistViewController.h"
#import "UIImageView+WebCache.h"
#import "GradientView.h"
#import "UIView+Frame.h"
#import "NetworkTool.h"

#import "CartoonBookDetailTableViewCell.h"
#import "CartoonBookDetailSummaryTableViewCell.h"
#import "CartoonShowViewController.h"

#import "CartoonBookDetaiData.h"
#import "CartoonBookDetai2Data.h"
#import "CartoonBookDetailModel.h"

#import "BookDetailHeaderData.h"
#import "BookDetailHeaderLabel.h"
#import "BookDetailHeaderModel.h"

#define kCartoonBookDetailTableViewCellReuse @"CartoonBookDetailTableViewCellReuse"

#define kCartoonBookDetailSummaryTableViewCellReuse @"CartoonBookDetailSummaryTableViewCellReuse"
@interface CartoonDetailLIistViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *mainImage;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headerVIew;

@property (weak, nonatomic) IBOutlet UIView *gradientView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UILabel *authorLabel;

@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)CartoonBookDetailModel *bookModel;

@property (nonatomic,strong)NSMutableArray *headerArray;
@property (nonatomic,strong)BookDetailHeaderModel *headerModel;

@end

@implementation CartoonDetailLIistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _dataArray = [NSMutableArray array];
    _headerArray = [NSMutableArray array];
    
    [self hanldeData];
    [self handleHeaderData];
        [_tableView registerNib:[UINib nibWithNibName:@"CartoonBookDetailTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCartoonBookDetailTableViewCellReuse];
    [_tableView registerNib:[UINib nibWithNibName:@"CartoonBookDetailSummaryTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCartoonBookDetailSummaryTableViewCellReuse];
    
    GradientView *gradient = [[GradientView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width , _gradientView.JH_height) type:TRANSPARENT_GRADIENT_TYPE];
//    gradient.alpha = 1;
    [_gradientView addSubview:gradient];
    

}

- (void)handleHeaderData{
    NSString *str1 = @"http://dajiaochong.517w.com/dacu_app/app/?c=BookDetail&a=get_book_info&id=";
    NSString *str2 = @"&ui=0&ui_id=0&userid=0";
    
    
    NSString *urlString = [[str1 stringByAppendingString:_bookId] stringByAppendingString:str2];
    
    
    NSDictionary *header = @{@"User-Agent":@"kidstone.cn/2.3.0/10/iPad/iOS 9.3.2/iPad/AC4A678F-C131-43C6-9024-873D3BD1A130/kidstone"};
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        NSLog(@"+_+_+_%@",[result[@"data"] objectForKey:@"cdn"]);
        
        _headerModel = [BookDetailHeaderModel modelWithDic:result];
        
        [_headerArray addObject:_headerModel];

        _authorLabel.text = _headerModel.data.author;
        _titleLabel.text = _headerModel.data.title;
        NSURL *url = [NSURL URLWithString:_headerModel.data.thumb_2];
        [_mainImage sd_setImageWithURL:url];


        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];



}



- (void)hanldeData{
    NSString *str1 = @"http://dajiaochong.517w.com/dacu_app/app/?c=BookDetail&a=get_chapter_list&view_type=1&id=";
    NSString *str2 = @"&ui=0&ui_id=0&userid=0&view_type=1";
    

//    NSString *urlString = @"http://dajiaochong.517w.com/dacu_app/app/?c=BookDetail&a=get_chapter_list&view_type=1&id=2335&ui=0&ui_id=0&userid=0&view_type=1";
    
//http://dajiaochong.517w.com/dacu_app/app/?c=BookDetail&a=get_book_info&id=1943&ui=0&ui_id=0&userid=0
    
    
    
    

    NSString *urlString = [[str1 stringByAppendingString:_bookId] stringByAppendingString:str2];
    
    
    NSDictionary *header = @{@"User-Agent":@"kidstone.cn/2.3.0/10/iPad/iOS 9.3.2/iPad/AC4A678F-C131-43C6-9024-873D3BD1A130/kidstone"};
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        NSLog(@"+_+_+_%@",[result[@"data"] objectForKey:@"cdn"]);
        
        _bookModel = [CartoonBookDetailModel modelWithDic:result];

        [_dataArray addObject:_bookModel];
        
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];

}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        return 150;
    }else{
    
        return 100;
    
    }

}




-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0) {
        return 1;

    }else{
        return _bookModel.data.data.count;

    }


}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    if (indexPath.section == 0) {
        CartoonBookDetailSummaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCartoonBookDetailSummaryTableViewCellReuse];
        cell.backgroundColor = [UIColor clearColor];
        UIImageView *backImageView=[[UIImageView alloc]initWithFrame:cell.contentView.bounds];
        backImageView.backgroundColor = [UIColor whiteColor];
        cell.backgroundView = backImageView;
        cell.backgroundView.alpha = 0.9;
        cell.summaryTextView.text = _headerModel.data.descriptionla;
        return cell;
    }
    
    else{
    
        CartoonBookDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCartoonBookDetailTableViewCellReuse];
        
        cell.backgroundColor = [UIColor clearColor];
        UIImageView *backImageView=[[UIImageView alloc]initWithFrame:cell.contentView.bounds];
        backImageView.backgroundColor = [UIColor whiteColor];
        cell.backgroundView = backImageView;
        cell.backgroundView.alpha = 0.9;
        cell.timeLabel.text = _bookModel.data.data[indexPath.row].time;
        cell.titleLabel.text = _bookModel.data.data[indexPath.row].name;
        NSString *str = @"http://cdn.517w.com//";
        NSString *urlStr = [str stringByAppendingString:_bookModel.data.data[indexPath.row].thumb];
        
        NSURL *url = [NSURL URLWithString:urlStr];
        [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];
        return cell;
    }
    
}






- (IBAction)handleBack:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        CartoonShowViewController *show = [[CartoonShowViewController alloc] init];
        show.cidString = _bookModel.data.data[indexPath.row].cid;
        [self presentViewController:show animated:YES completion:^{
            
        }];
    }
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
